<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    <>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_transport_track.class.php 2011-07-11 18:36:35 $
 * @Show Note: 
 */
Class zy_transport_track extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_transport_track()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_transport_track.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_transport_track");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('transport_type_arr',get_transport_type());//运输方式
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_transport_track();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "transport_track_id";
		$where = 1;
		//运货单号检索
		$transport_no = isset($_GET['transport_no']) ? trim($_GET['transport_no']) : '';
		if (!empty($transport_no)) {
			$where .= " AND transport_no like '%{$transport_no}%' ";
		}
		//项目检索
		$project_id = isset($_GET['project_id']) ? trim($_GET['project_id']) : '';
		if (!empty($project_id)) {
			$where .= " AND project_id = {$project_id} ";
		}
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		//取得运输跟踪记录信息
		/*$transport_arr = array();
		foreach ($arr as $value)
		{
			$transport_arr[$value['transport_track_id']] = $this->get_transport_arr($value['transport_track_id']);//根据运输ID取得信息
		}*/
		/*$this->smarty->assign('transport_arr',$transport_arr);*/
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_transport_track($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, 'index.php?c=zy_transport_track');
			}else{
				redirectPage('index.php?c=zy_transport_track');
			}
			//alertMsg($error_no, 'index.php?c=zy_transport_track');
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$transport_track_id = isset($_REQUEST['transport_track_id']) ? intval(trim($_REQUEST['transport_track_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($transport_track_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_transport_track');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "transport_track_id=$transport_track_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_transport_track($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, 'index.php?c=zy_transport_track');
			}else{
				redirectPage('index.php?c=zy_transport_track');
			}
			//alertMsg($error_no, 'index.php?c=zy_transport_track');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$transport_track_id = isset($_REQUEST['transport_track_id']) ? intval(trim($_REQUEST['transport_track_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($transport_track_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_transport_track');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		//取得运输跟踪记录信息
		$transport_arr = $this->get_transport_arr($transport_track_id);//根据运输ID取得信息
		//分页，通过查询所有来去
		$rowNo = count($transport_arr);
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		//unset($zy_plan_reply_mc_arr);
		for ($i=$offset;$i<($numrows+$offset);$i++){
			if(isset($transport_arr[$i]))
			$page_arr[] = $transport_arr[$i];
		}
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		
		$this->smarty->assign('recordArr',$page_arr);
		$this->smarty->assign('transport_arr',$page_arr);
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->assign('transport_track_id',$transport_track_id);
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$transport_track_id = isset($_REQUEST['transport_track_id']) ? intval(trim($_REQUEST['transport_track_id'])) : 0 ;
		//查看是否有运输记录如果有提示后退出
		$temp = $this->get_transport_arr($transport_track_id);
		if (!empty($temp)) {
			alertMsg('-5013');
		}
		$where = "transport_track_id=$transport_track_id";
		$del=$this->db->delete_zy_transport_track($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, 'index.php?c=zy_transport_track');
		}else{
			redirectPage('index.php?c=zy_transport_track');
		}
		//alertMsg($error_no, 'index.php?c=zy_transport_track');
	}

	/**
	 *	import method.
	 */
	public function imports()
	{
		$opt = isset($_POST['opt']) ? intval($_POST['opt']) : 0 ;
		if(empty($opt))	{
			$this->smarty->assign('act_title', '导入数据');
			$this->smarty->assign('act', 'imports');
			$this->smarty->assign('control', 'zy_transport_track');
			$this->smarty->display($this->tplFile);
		} else {
			//1. 上传xls
			$savePath = UPLOAD_PATH . 'csv/zy_transport_track/';
			if(!file_exists($savePath)) {
				mk_dir($savePath);
			}
			$uploadRes = $this->upload_file_xls($savePath, 'fileName', array('csv'));
			if(0 == $uploadRes['state']) {
				alertMsg2($uploadRes['msg'], 'index.php?c=zy_transport_track');
			}
			$fileName = $uploadRes['msg']; //上传成功后文件名

			//2. 上传成功后读取xls数据分析
			$item_pe = Factory::getItemClass('parse_excel');

			//获取xls文件数据
			$filePath = $savePath.$fileName;
			//$arr_xls = $item_pe->data_xls($filePath);
			//$out = $item_pe->data_parse_xls($arr_xls);

			$arr_csv = $item_pe->data_csv($filePath);
			$out = is_array($arr_csv['data']) ? $arr_csv['data'] : array();

			if(empty($out)) {
				alertMsg2("读取文件内容失败或数据为空", 'index.php?c=zy_transport_track');
			}

			unset($out[0]); //去掉标题栏

			//3. 入库
			$db_ztt = Factory::getDB('zy_transport_track');
			foreach($out as $k => $v) {
				if(!empty($v)) {
					if(!empty($v[0]) && !empty($v[1])) {
						$val['transport_no'] = $v[0];
						$val['materials_info'] = $v[1];
						$val['app_time'] = empty($v[2]) ? '' :  strtotime($v[2]);
						$val['reply_no'] =$v[3];
						$val['state'] = $v[4];
						$val['start_time'] = empty($v[5]) ? '' :  strtotime($v[5]);
						$val['v_end_time'] = empty($v[6]) ? '' :  strtotime($v[6]);
						$val['end_time'] = empty($v[7]) ? '' :  strtotime($v[7]);
						$val['info'] = $v[8];
						$val['type_id'] = $v[9];
						$val['project_id'] = $v[10];
						
						$resId = $db_ztt->insert_zy_transport_track($val);
					}
				}
			}
			//redirectPage('index.php?c=zy_mend');
			alertMsg2('导入完成', 'index.php?c=zy_transport_track');
		}
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['transport_track_id'] = $_POST['transport_track_id'];
		} else {
			$temp['transport_track_id'] = "";
		}
		//补充其他的字段
				$temp['transport_no'] = isset($_POST['transport_no']) ? htmlspecialchars($_POST['transport_no']) : '';//运输单号
				if (empty($temp['transport_no'])) {
					alertMsg('-5007');
				}
				//查重复
				if ($this->db->check_transport_no_repeat($temp['transport_no'],$temp['transport_track_id']) == 1) {
					alertMsg('-5008');	
				}
				//查重复
				$temp['materials_info'] = isset($_POST['materials_info']) ? htmlspecialchars($_POST['materials_info']) : '';//物资描述
				$temp['app_time'] = isset($_POST['app_time']) ? strtotime($_POST['app_time']) : time();//申请日期
				$temp['reply_no'] = isset($_POST['reply_no']) ? htmlspecialchars($_POST['reply_no']) : '';//审批单号
				$temp['state'] = isset($_POST['state']) ? intval($_POST['state']) : 1;//状态
				$temp['start_time'] = isset($_POST['start_time']) ? strtotime($_POST['start_time']) : time();//申请日期//启运时间
				$temp['v_end_time'] =isset($_POST['v_end_time']) ? strtotime($_POST['v_end_time']) : time();//预计到货时间
				$temp['end_time'] = isset($_POST['end_time']) ? strtotime($_POST['end_time']) : time();//到货时间
				$temp['info'] = isset($_POST['info']) ? htmlspecialchars($_POST['info']) : '';//审批单号//备注
				$temp['type_id'] = isset($_POST['type_id']) ? intval($_POST['type_id']) : 0;//运输方式
				$temp['project_id'] = isset($_POST['project_id']) ? intval($_POST['project_id']) : 1;//项目
				$temp['all_price'] = isset($_POST['all_price']) ? trim($_POST['all_price']) : '';//总价
				$temp['purchase_user'] = isset($_POST['purchase_user']) ? trim($_POST['purchase_user']) : '';//负责人
				$$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
				//判断是否存在文件上传
				if (!empty($file_path['name']) && !empty($temp['transport_no'])) {
					$re = upload_file($_FILES,'file_path',$temp['transport_no']);
					$temp['file_path'] = $re;//获得上传文件名称
				}//上传
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'运输跟踪单管理'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'imports'  => array('imports','导入',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
	/**
	 * 根据运输ID取得信息数据
	 *
	 * @param unknown_type $transport_track_id
	 * @return unknown
	 */
	function get_transport_arr($transport_track_id)
	{
		$zy_transport_track_list_db = Factory::getDB('zy_transport_track_list');
		return $zy_transport_track_list_db->get_info_by_transport_track_id($transport_track_id);
	}
}

?>